/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Klijent.Clipboard;
import Klijent.Komunikacija;
import Server.Korisnik;
import Strukture.Flare;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author igor
 */
public class LoginForma extends javax.swing.JFrame {

    /**
     * Creates new form LoginForma
     */
    public LoginForma() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Korisnicko ime:");

        jLabel2.setText("Sifra:");

        jButton1.setBackground(javax.swing.UIManager.getDefaults().getColor("Nb.ScrollPane.Border.color"));
        jButton1.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jButton1.setText("Uloguj se");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(280, 280, 280))
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1)
                    .addComponent(jPasswordField1))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(jLabel2)
                .addGap(12, 12, 12)
                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(0, 28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

         /*
        *algoritam koji pretrazuje korisnicko ime i sifru i ukoliko
        *se poklapaju sa odredjenim parom iz "baze",
        *vraca pocetnu stranu i omogucava sinhronizaciju
        *(prethodno treba onemoguciti sinhronizaciju u glavnoj formi). 
        */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String korisnickoIme = jTextField1.getText();
        String sifra = new String (jPasswordField1.getPassword());
        if (proveriDaLiJePrazno(korisnickoIme, sifra) == true){
            dispose();
        } else {
            Korisnik korisnikIlija = new Korisnik(korisnickoIme, sifra);
            Flare raketa = new Flare(null);
            try {
                //parametar 2 znaci da pozivamo deo metode koji proverava da li su ok parametri i loguje korisnika
                Komunikacija.communication(korisnikIlija, 2, null, raketa);
            } catch (IOException ex) {
                Logger.getLogger(LoginForma.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println(raketa.tekstZaPrenos);

            JOptionPane.showMessageDialog(this, raketa.tekstZaPrenos);
        }
        
//        try {
//            
//            BufferedReader bf = new BufferedReader(new FileReader(korisnik+".txt"));
//            
//            String line = bf.readLine();
//            
//            int prvaRec = line.indexOf(" ");
//            int drugaRec = line.indexOf(" ", prvaRec + 1);
//            if(korisnik.equals(line.substring(0,prvaRec))
//                                 && sifra.equals(line.indexOf(" "+1,drugaRec))){
//                
//                Korisnik user = new Korisnik(korisnik, sifra);
////                user.setIme(korisnik);
////                user.setPassword(sifra);
//                user.setLokacijaKorisnickogFoldera(korisnik);
//                user.setID('0');
//                
//                
//                new GlavnaForma().setVisible(true);
//            }else{
//                JOptionPane.showMessageDialog(this, "Korisnicko ime ili sifra nisu ispravni!");
//            }      
//        } catch (FileNotFoundException ex) {
//            Logger.getLogger(LoginForma.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IOException ex) {
//            Logger.getLogger(LoginForma.class.getName()).log(Level.SEVERE, null, ex);
//        } 
       
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void OtvoriLogin() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoginForma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoginForma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoginForma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoginForma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LoginForma().setVisible(true);
            }
        });
    }
    
    /**
     * metoda koja proverava ako korisnik nije popunio sva polja i opominje ga da sva polja 
     * moraju biti popunjena
     * @return false oznacava da polja nisu prazna a true da jesu...
     */
    public boolean proveriDaLiJePrazno(String korisnickoIme, String sifraKorisnika){
        
        //trim() se poziva da bi se onemogucio korisnik koji zaspi na tastaturi da napise: "                 "
        if (korisnickoIme.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Polje korisnicko ime ne sme da bude prazno");
                return true;
        }
        
        if (sifraKorisnika.trim().equals("")){
                JOptionPane.showMessageDialog(this, "Polje sifra korisnika ne sme da bude prazno");
                return true;
        }
        return false;
        
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}

